<template>
<div class="top-bar">
  <div class="menu">
    <img src="@/assets/image/home/home-logo-white.png" class="logo" @click="changeMenu(0, '/')"/>
    <div class="menu-row">
      <div @click="changeMenu(index, item.path)"
        v-for="(item, index) in [{name:'首页', path: '/'}, {name:'节目单', path: '/program-list'}, {name:'热门作品',path: '/video-library'}, {name:'创作者地图',path: '/china-map'}]" :key="index" 
        class="menu-item" :class="activeIdx==index?'active' :''">
        {{ item.name }}
      </div>
      <div ref="buttonRef" v-click-outside="onClickOutside" class="menu-item">商务合作</div>
    </div>
    <span class="welcome">欢迎回来，AI春晚创作者 <span class="user-name">{{ loginStore.userinfo.nickname }}</span></span>
    <button class="login_btn" @click="showLogin()" v-if="!loginStore.userinfo.nickname">登录</button>
    <el-dropdown :hide-on-click="false">
      <el-avatar :size="40" :src="loginStore.userinfo.avatar" v-if="loginStore.userinfo.nickname" class="avatar"></el-avatar>
      <template #dropdown>
        <el-dropdown-menu class="dropdown-menu">
          <el-dropdown-item @click="homepage()" style="display: block;margin-bottom: 5px;">个人主页</el-dropdown-item>
          <el-dropdown-item @click="logout()">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
    <!-- 商务合作弹框 -->
    <el-popover ref="popoverRef" width="352px" popper-style="padding:0;border-radius: 10px; height:412px;"
      :virtual-ref="buttonRef"
      trigger="click"
      virtual-triggering>
      <img src="@/assets/image/home/contact2.png" width="350px"/>
    </el-popover>
  </div>
</div>
</template>

<script setup>
import { ref, unref, onMounted } from "vue";
import { useRouter } from 'vue-router';
import { ClickOutside as vClickOutside } from 'element-plus'

import emitter from '../utils/eventBus';

import { useLoginStore } from '../api/store'; 

const loginStore = useLoginStore();
const router = useRouter();
const activeIdx = ref(0);


// 商务合作
const buttonRef = ref()
const popoverRef = ref()
const onClickOutside = () => {
  unref(popoverRef).popperRef?.delayHide?.()
}

const changeMenu = (idx, path) => {
  activeIdx.value = idx;  
  router.push(path);
}

const fetchUserInfo = async() => {
 await loginStore.fetchUserInfo();  
}

const showLogin = () => {
  emitter.emit('open-auth-modal')
}

const logout = () => {
  loginStore.logout()
  router.push('/')
  // emitter.emit('open-auth-modal')
}

const homepage = () => {
  router.push('/homepage')
}

onMounted(() => {
  fetchUserInfo()
})
</script>

<style lang="less">
.dropdown-menu {
  padding: 10px!important;
  cursor: pointer;
}
</style>

<style lang="less" scoped>
.top-bar {
  background: #820000;
  color: #fff;
  .menu {
    width: fit-content;
    padding-top: 19px;
    padding-bottom: 19px;
    margin-left: auto;
    margin-right: auto;     
    display: flex;
    align-items: center;      
    .logo {
      width: 140px;
      height: 41px;
      margin-right: 65px;
      cursor: pointer;
    }
    .menu-row {
      margin-right: 115px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .menu-item {
        cursor: pointer;
        color: #EDEDED;
      }
      .menu-item + .menu-item {
        margin-left: 32px;
      }
      .active {
        color: #fff;
        font-weight: 700;
      }
    }
    .welcome {
      font-weight: 500;
      margin-right: 32px;
      .user-name {
        margin-left: 10px;
        font-weight: 700;
      }
    }
    .login_btn {
      background: #fff;
      border-radius: 8px;
      border: 0;
      padding: 10px 36px;
      font-weight: 700;
      cursor: pointer;      
    }
    .avatar {
      cursor: pointer;
    }
    
  }  
}
</style>